package com.q.tqb.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;
import com.q.tqb.R;
import com.q.tqb.activity.HtmlActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.BaseInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.TimeButton;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {

    @BindView(R.id.et_tel)
    public EditText et_tel;
    @BindView(R.id.et_code)
    public EditText et_code;

    @BindView(R.id.et_nick)
    public EditText et_nick;
    @BindView(R.id.et_pass)
    public EditText et_pass;
    @BindView(R.id.et_pass1)
    public EditText et_pass1;
    @BindView(R.id.et_referee)
    public EditText et_referee;
    @BindView(R.id.et_community)
    public EditText et_community;
    @BindView(R.id.rb_man)
    public RadioButton rb_man;
    @BindView(R.id.rb_women)
    public RadioButton rb_women;

    @BindView(R.id.tv_city)
    public TextView tv_city;
    @BindView(R.id.cb_agreement)
    public CheckBox cb_agreement;

    @BindView(R.id.btn_code)
    public TimeButton btn_code;
    //申明对象
    CityPickerView mPicker = new CityPickerView();
    public String cityCode;

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("注册");
        /**
         * 预先加载仿iOS滚轮实现的全部数据
         */
        mPicker.init(this);
    }


    @OnClick({R.id.iv_back, R.id.tv_agreement, R.id.btn_code, R.id.tv_city, R.id.regist_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_agreement:
                Intent intent = new Intent(RegisterActivity.this, HtmlActivity.class);
                intent.putExtra("title", "用户协议");
                intent.putExtra("url", "http://m.tqboo.com/home/help/xieyi");
                startActivity(intent);
                break;
            case R.id.btn_code:
                getPhoneCode();
                break;
            case R.id.tv_city:
                showCityPicker();
                break;
            case R.id.regist_btn:
                register();
                break;
        }
    }

    private void register() {
        if (!cb_agreement.isChecked()) {
            showToast("请先同意淘趣包协议");
            return;
        }
        String phone = et_tel.getText().toString().trim();
        if (!StringUtil.isMobile(phone)) {
            showToast("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(et_code.getText().toString())) {
            showToast("请输入验证码");
            return;
        }

        if (TextUtils.isEmpty(et_nick.getText().toString())) {
            showToast("请输入昵称");
            return;
        }

        if (TextUtils.isEmpty(et_pass.getText().toString())) {
            showToast("请输入登录密码");
            return;
        }
        if (TextUtils.isEmpty(et_pass1.getText().toString())) {
            showToast("请输入确认密码");
            return;
        }
        if (!et_pass1.getText().toString().equals(et_pass.getText().toString())) {
            showToast("两次密码不一致，请重新输入");
            return;
        }
        if (TextUtils.isEmpty(tv_city.getText().toString()) || TextUtils.isEmpty(cityCode)) {
            showToast("请选择城市");
            return;
        }
        if (!rb_man.isChecked() && !rb_women.isChecked()) {
            showToast("请选择性别");
        }
        String s = "1";
        if (rb_women.isChecked()) {
            s = "2";
        }
        Map<String, String> map = new HashMap<>();
        final String mobile = et_tel.getText().toString().trim();
        final String pwd = et_pass.getText().toString().trim();
        String pwd1 = et_pass1.getText().toString().trim();

        map.put("nickname", et_nick.getText().toString());
        map.put("mobile", mobile);
        map.put("password", pwd);
        map.put("confirm_password", pwd1);
        map.put("sex", s);
        map.put("cityid", cityCode);
        map.put("referee_uid", et_referee.getText().toString());
        map.put("community_uid", et_community.getText().toString());
        map.put("code", et_code.getText().toString().trim());
        mProgressDialog.show();

        OkHttpUtil.getInstance().post(this, AppConst.URL_REGISTER, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                BaseInfo personData = gson.fromJson(result, BaseInfo.class);
                showToast(personData.getMsg());
                mProgressDialog.dismiss();
                reSetOk(mobile, pwd);
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });

    }

    private void reSetOk(String mobile, String pwd) {
        Intent intent = new Intent();
        intent.putExtra("mobile", mobile);
        intent.putExtra("pwd", pwd);
        setResult(RESULT_OK, intent);
        RegisterActivity.this.finish();
    }

    private void showCityPicker() {
        //添加默认的配置，不需要自己定义
        CityConfig cityConfig = new CityConfig.Builder().build();
        cityConfig.setDefaultProvinceName("北京");
        cityConfig.setDefaultCityName("北京");
        cityConfig.setDefaultDistrict("西城区");
        cityConfig.setShowGAT(true);
        cityConfig.setProvinceCyclic(false);//省份滚轮是否可以循环滚动
        cityConfig.setCityCyclic(false);//城市滚轮是否可以循环滚动
        cityConfig .setDistrictCyclic(false);//区县滚轮是否循环滚动
        mPicker.setConfig(cityConfig);

        //监听选择点击事件及返回结果
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                //省份
                if (province != null && city != null && district != null) {
                    tv_city.setText(province.getName() + city.getName() + district.getName());
                    cityCode = district.getId();
                }
            }

            @Override
            public void onCancel() {

            }
        });

        //显示
        mPicker.showCityPicker();
    }

    private void getPhoneCode() {
        String phone = et_tel.getText().toString().trim();
        if (!StringUtil.isMobile(phone)) {
            showToast("请输入正确的手机号");
            return;
        }
        btn_code.startTimer();
        Map<String, String> map = new HashMap<>();
        map.put("mobile", phone);
        map.put("type", "1");
        mProgressDialog.show();
        OkHttpUtil.getInstance().post(this, AppConst.URL_GET_PHONE_CODE, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                BaseInfo personData = gson.fromJson(result, BaseInfo.class);
                showToast(personData.getMsg());
                mProgressDialog.dismiss();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });
    }
}
